package jp.co.withone.gadget.putiputi.base;

import jp.co.withone.gadget.putiputi.setting.PutiSettingView;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class PutiPutiActivator implements BundleActivator, SettingViewListener {

	private ServiceRegistration putiReg;
	private Object lock = new Object();
	private boolean stopped=true;
	
	private PutiBody body;

	private PutiSettingView settingView;
	
	@Override
	public void start(BundleContext context) throws Exception {
		
		synchronized (this.lock) {
		
			stopped = false;
			
			this.putiReg = context.registerService(SettingViewListener.class.getName(), this, null);
			
			this.body = new PutiBody();
			this.body.run();
			
			this.settingView = new PutiSettingView();
		}
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		
		synchronized (this.lock) {
			
			this.body.clear();
			this.body = null;		
			
			this.putiReg.unregister();
			this.putiReg = null;
			
			this.settingView.dispose();
			
			stopped = true;
		}
	}

	@Override
	public void show(int x, int y) {

		if(stopped){
			return;
		}

		this.settingView.setBounds(x, y, 50, 70);
		this.settingView.setVisible(true);
	}

}
